﻿using System.Collections.Generic;
using System.Linq;

using VirtualTournaments.DataAccess.EntityManagers;
using VirtualTournaments.DataAccess.SqlServer.DataMappers;
using VirtualTournaments.DataAccess.SqlServer.Exceptions;

namespace VirtualTournaments.DataAccess.SqlServer.EntityManagers
{
	internal sealed class BetTypesManager : IBetTypesManager
	{
		#region Singleton implementation

		private static BetTypesManager _instance = null;
		private static readonly object _instanceLocker = new object();

		private BetTypesManager() { }

		public static BetTypesManager Instance
		{
			get
			{
				if (_instance == null)
				{
					lock (_instanceLocker)
					{
						if (_instance == null)
						{
							_instance = new BetTypesManager();
						}
					}
				}
				return _instance;
			}
		}

		#endregion

		#region IBetTypesManager Members

		public IEnumerable<Business.Bets.BetType> GetByCompetition(Business.Tournaments.ICompetition competition)
		{
			if (competition is Business.Tournaments.Match)
			{
				Business.Tournaments.Match match = (Business.Tournaments.Match)competition;
				DataProxies.MatchDataProxy matchDataProxy = MatchesManager.Instance.GetByTournamentAndLocalTeamAndVisitorTeam(match.Tournament, match.Local, match.Visitor) as DataProxies.MatchDataProxy;
				if (matchDataProxy == null)
					return new Business.Bets.BetType[0].AsEnumerable();
				return ThreeWaysBetTypesDataMapper.Instance.GetList(betType => betType.Kind == (int)BetTypeKind.ThreeWaysBetType && betType.MatchId == matchDataProxy.InnerDataObject.MatchId).AsEnumerable().Cast<Business.Bets.BetType>();
			}
			else if (competition is Business.Tournaments.Tournament)
			{
				Business.Tournaments.Tournament tournament = (Business.Tournaments.Tournament)competition;
				DataProxies.ITournamentDataProxy tournamentDataProxy = TournamentsManager.Instance.GetByExactName(tournament.Name) as DataProxies.ITournamentDataProxy;
				if (tournamentDataProxy == null)
					return new Business.Bets.BetType[0].AsEnumerable();
				return TournamentPodiumBetTypesDataMapper.Instance.GetList(betType => betType.Kind == (int)BetTypeKind.TournamentPodiumBetType && betType.TournamentId == tournamentDataProxy.InnerDataObject.TournamentId).AsEnumerable().Cast<Business.Bets.BetType>();
			}
			else
			{
				throw new InvalidCompetitionParamException();
			}
		}

		#endregion

		#region IEntityManager<BetType> Members

		public System.Collections.Generic.IEnumerable<Business.Bets.BetType> GetAll()
		{
			throw new System.NotImplementedException();
		}

		public void Save(Business.Bets.BetType entity)
		{
			if (entity is Business.Bets.ThreeWaysBetType)
			{
				ThreeWaysBetTypesDataMapper.Instance.Save((Business.Bets.ThreeWaysBetType)entity);
				ThreeWaysBetTypesDataMapper.Instance.GetDataContext().SubmitChanges();
			}
			else if (entity is Business.Bets.TournamentPodiumBetType)
			{
				throw new System.NotImplementedException();
			}
			else
			{
				throw new InvalidCompetitionParamException();
			}
		}

		public void Delete(Business.Bets.BetType entity)
		{
			throw new System.NotImplementedException();
		}

		#endregion
	}
}
